package com.qiyi.player.core.model.remote
{
   import com.qiyi.player.base.rpc.impl.BaseRemoteObject;
   import com.qiyi.player.core.player.coreplayer.ICorePlayer;
   import com.qiyi.player.base.logging.ILogger;
   import flash.net.URLRequest;
   import com.qiyi.player.core.Config;
   import com.qiyi.player.core.model.utils.CachePool;
   import com.qiyi.player.core.player.def.PlayerUseTypeEnum;
   import com.qiyi.player.base.pub.ProcessesTimeRecord;
   import flash.utils.getTimer;
   import flash.events.Event;
   import flash.utils.clearTimeout;
   import com.adobe.serialization.json.JSON;
   import com.qiyi.player.base.rpc.RemoteObjectStatusEnum;
   import com.qiyi.player.base.logging.Log;
   
   public class RequestVRSInfoRemote extends BaseRemoteObject
   {
      
      public function RequestVRSInfoRemote(param1:ICorePlayer) {
         this._log = Log.getLogger("com.qiyi.player.core.model.remote.RequestVRSInfoRemote");
         super(0,"RequestVRSInfoRemote");
         _timeout = Config.VRS_INFO_TIMEOUT;
         _retryMaxCount = Config.VRS_INFO_MAX_RETRY;
         this._holder = param1;
      }
      
      private var _holder:ICorePlayer;
      
      private var _log:ILogger;
      
      override protected function getRequest() : URLRequest {
         var _loc2_:String = null;
         var _loc3_:String = null;
         var _loc1_:* = Config.VRS_INFO_SERVER_URL + this._holder.runtimeData.tvid + "/" + this._holder.runtimeData.originalVid + "/";
         if(this._holder.runtimeData.vrsDomain != "")
         {
            _loc2_ = this._holder.runtimeData.vrsDomain;
            _loc2_ = _loc2_.substr(7);
            _loc2_ = _loc2_.substring(0,_loc2_.indexOf("/"));
            _loc3_ = "http://" + _loc2_ + "/";
            _loc1_ = _loc1_.replace(new RegExp("http:\\/\\/(\\w|\\.)*\\/"),_loc3_);
         }
         return new URLRequest(_loc1_);
      }
      
      override public function initialize() : void {
         var _loc1_:String = CachePool.getVRSInfoSource(this._holder.runtimeData.tvid,this._holder.runtimeData.originalVid);
         if((this._holder.runtimeData.playerUseType == PlayerUseTypeEnum.PREVIEW || this._holder.runtimeData.playerUseType == PlayerUseTypeEnum.SHARE_SECTION) && (_loc1_))
         {
            this.onComplete(null);
         }
         else
         {
            ProcessesTimeRecord.STime_VI = getTimer();
            super.initialize();
         }
      }
      
      override protected function onComplete(param1:Event) : void {
         var status:int = 0;
         var event:Event = param1;
         clearTimeout(_waitingResponse);
         _waitingResponse = 0;
         try
         {
            status = 0;
            if(event)
            {
               ProcessesTimeRecord.usedTime_VI = getTimer() - ProcessesTimeRecord.STime_VI;
               _data = _loader.data;
               status = int(com.adobe.serialization.json.JSON.decode(_data as String).st);
               if(status != 200)
               {
                  setStatus(RemoteObjectStatusEnum.AuthenticationError);
               }
               else
               {
                  CachePool.saveVRSInfoSource(this._holder.runtimeData.tvid,this._holder.runtimeData.originalVid,_data as String);
                  super.onComplete(event);
               }
            }
            else
            {
               this._log.debug("use cache pool vrs info source,tvid:" + this._holder.runtimeData.tvid + ",vid:" + this._holder.runtimeData.originalVid);
               _data = CachePool.getVRSInfoSource(this._holder.runtimeData.tvid,this._holder.runtimeData.originalVid);
               status = int(com.adobe.serialization.json.JSON.decode(_data as String).st);
               if(status != 200)
               {
                  setStatus(RemoteObjectStatusEnum.AuthenticationError);
               }
               else
               {
                  super.onComplete(event);
               }
            }
         }
         catch(e:Error)
         {
            setStatus(RemoteObjectStatusEnum.DataError);
         }
      }
   }
}
